#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "file_handler.h"
#include "draw_chart.h"
#include "error_process.h"

#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QStatusBar>
#include <QVBoxLayout>
#include <QSplitter>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    fileHandler(new FileHandler(this)),
    drawChart(new DrawChart(this)),
    errorMessage(new ErrorMessage(this))
{
    ui->setupUi(this);
    setupUI();
    setupMenu();

    // 连接信号槽
    connect(fileHandler, &FileHandler::fileLoaded, this, [this](const QString &content, const QString &filePath) {
        fileContent = content;
        currentFilePath = filePath;
        updateStatus("文件加载成功: " + filePath);

        // 不再自动绘制图表，只是解析数据
        drawChart->parseData(content);
        updateStatus("数据解析完成，请选择要绘制的图表类型");
    });

    connect(fileHandler, &FileHandler::errorOccurred, this, [this](const QString &error) {
        errorMessage->showError(this, error);
        updateStatus("错误: " + error);
    });

    connect(drawChart, &DrawChart::chartDrawn, this, [this]() {
        updateStatus("图表生成完成");
    });

    connect(drawChart, &DrawChart::errorOccurred, this, [this](const QString &error) {
        errorMessage->showError(this, error);
        updateStatus("图表错误: " + error);
    });

    qDebug() << "MainWindow 初始化完成";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setupUI()
{
    // 设置窗口属性
    setWindowTitle("数据图表分析工具 - Qt 6.9.2");
    resize(1200, 800);

    qDebug() << "UI 设置完成";
}

void MainWindow::setupMenu()
{
    // 文件菜单
    QMenu *fileMenu = menuBar()->addMenu("文件");

    QAction *openAction = fileMenu->addAction("打开");
    QAction *saveAction = fileMenu->addAction("保存");
    QAction *saveAsAction = fileMenu->addAction("另存为");
    fileMenu->addSeparator();
    QAction *saveChartAction = fileMenu->addAction("保存图表为图片");
    fileMenu->addSeparator();
    QAction *exitAction = fileMenu->addAction("退出");

    // 新增：绘制菜单
    QMenu *drawMenu = menuBar()->addMenu("绘制");

    QAction *drawS_BMSAction = drawMenu->addAction("绘制 S_BMS 图表");
    QAction *drawBMSFAction = drawMenu->addAction("绘制 BMS F 图表");
    QAction *drawAllAction = drawMenu->addAction("绘制所有图表");
    drawMenu->addSeparator();
    QAction *clearChartsAction = drawMenu->addAction("清除图表");

    // 连接信号槽
    connect(openAction, &QAction::triggered, this, &MainWindow::onOpenFile);
    connect(saveAction, &QAction::triggered, this, &MainWindow::onSaveFile);
    connect(saveAsAction, &QAction::triggered, this, &MainWindow::onSaveAs);
    connect(saveChartAction, &QAction::triggered, this, &MainWindow::onSaveChartAsImage);

    // 新增：连接绘制菜单的信号槽
    connect(drawS_BMSAction, &QAction::triggered, this, &MainWindow::onDrawS_BMSChart);
    connect(drawBMSFAction, &QAction::triggered, this, &MainWindow::onDrawBMSFChart);
    connect(drawAllAction, &QAction::triggered, this, &MainWindow::onDrawAllCharts);
    connect(clearChartsAction, &QAction::triggered, this, &MainWindow::clearCharts);

    connect(exitAction, &QAction::triggered, this, &QMainWindow::close);

    // 状态栏
    statusBar()->showMessage("就绪");
    qDebug() << "菜单设置完成";
}

void MainWindow::onOpenFile()
{
    qDebug() << "打开文件操作";
    fileHandler->openFile(this);
}

void MainWindow::onSaveFile()
{
    qDebug() << "保存文件操作";
    if (currentFilePath.isEmpty()) {
        onSaveAs();
    } else {
        fileHandler->saveFile(currentFilePath, fileContent);
    }
}

void MainWindow::onSaveAs()
{
    qDebug() << "另存为操作";
    fileHandler->saveAsFile(this, fileContent);
}

void MainWindow::onSaveChartAsImage()
{
    qDebug() << "保存图表为图片操作";
    drawChart->saveChartsAsImage(this);
}

// 新增：绘制 S_BMS 图表
void MainWindow::onDrawS_BMSChart()
{
    qDebug() << "绘制 S_BMS 图表";
    if (fileContent.isEmpty()) {
        errorMessage->showError(this, "请先打开数据文件");
        return;
    }

    QWidget *centralWidget = this->centralWidget();
    if (centralWidget) {
        drawChart->drawS_BMSChart(centralWidget);
    } else {
        errorMessage->showError(this, "无法获取中央部件");
    }
}

// 新增：绘制 BMS F 图表
void MainWindow::onDrawBMSFChart()
{
    qDebug() << "绘制 BMS F 图表";
    if (fileContent.isEmpty()) {
        errorMessage->showError(this, "请先打开数据文件");
        return;
    }

    QWidget *centralWidget = this->centralWidget();
    if (centralWidget) {
        drawChart->drawBMSFChart(centralWidget);
    } else {
        errorMessage->showError(this, "无法获取中央部件");
    }
}

// 新增：绘制所有图表
void MainWindow::onDrawAllCharts()
{
    qDebug() << "绘制所有图表";
    if (fileContent.isEmpty()) {
        errorMessage->showError(this, "请先打开数据文件");
        return;
    }

    QWidget *centralWidget = this->centralWidget();
    if (centralWidget) {
        drawChart->drawAllCharts(centralWidget);
    } else {
        errorMessage->showError(this, "无法获取中央部件");
    }
}

// 新增：清除图表
void MainWindow::clearCharts()
{
    qDebug() << "清除图表";
    QWidget *centralWidget = this->centralWidget();
    if (centralWidget) {
        drawChart->clearCharts(centralWidget);
        updateStatus("图表已清除");
    }
}

void MainWindow::updateStatus(const QString &message)
{
    statusBar()->showMessage(message);
    qDebug() << "状态更新:" << message;
}
